/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// 将区间划分为两部分
// [l1, r1] [r1 + 1, nums.length - 1]
var search = function(nums, target) {
  let l = 0, r = nums.length - 1
   let temp = nums[0]
   while(l <= r) {
     let mid = (l + r) >> 1
     if(nums[mid] >= temp) {
       l = mid + 1
     } else if(nums[mid] < temp) {
       r = mid - 1
     }
   }
   // r是第二段最小值的下标
   // 如果取r作为边界，当数组完全递增时（没有这个下标，所以取值取l）
   if(target >= nums[0]) {
     r = l - 1
     l = 0
   } else {
     r = nums.length - 1
   }
   while(l <= r) {
     let mid = (l + r) >> 1
     if(target == nums[mid]) return mid
     else if(target > nums[mid]) l = mid + 1
     else r = mid - 1
   }
   return -1
 };
console.log(search([3, 1], 3));
console.log(search([1, 3], 3));
console.log(search([1], 1));